Skip to content

Add Arial AFM and modify fontmetric.h (issue #10)#11

Open
bill-huang26 wants to merge 2 commits intoplfjohnson:mainfrom
bill-huang26:main
Open

Add Arial AFM and modify fontmetric.h (issue #10)#11
bill-huang26 wants to merge 2 commits intoplfjohnson:mainfrom
bill-huang26:main

Conversation

@bill-huang26
Copy link

Hi Johnson,
Thanks for creating this package. This is very useful for image outputting.
I'm Bill and I work in CRO as an application programmer. Now I'm also a co-author of reporter package.

I found that Arial font is useful for pharmaceutical company. However, even though libconfig package is installed, Arial font might not be in the system. That's why I create issue #10.

I have modified the package, please review it and consider if it's okay to be implemented.
As for the details about my approach, please see below information.

(1.) Create Arial AFM (Adobe Font Metric) file with FontForge app

(2.) Put the AFM files into the existed inst folder in devEMF package
image

Then add them into afmPathDB in fontmetric.h:
image

(3.) Modify code the let devEMF read the Arial AFM file

Though we put Arial AFM files into inst/, it still won’t read them because we have libfontconfig-dev in our system, but we don’t have Arial font, then we always get the warning message such like:
devEMF: your system substituted font family ' Helvetica' when you requested 'Arial'

I think that's because when libfontconfig exists, the package substituted the similar font from libfontconfig font metric information instead of loading the AFM files from inst/.

So I modify the code in fontmetric.h. If the font is not in libfontconfig font metric, then it will import AFM file.
image

Then because the algorithm to read AFM is based on hexadecimal, we need to add decimal for Arial AFM.
image

(4.) Read and output Arial successfully
You can run below code for testing.

library(ggplot2)
library(reporter)
library(devEMF)
dat <- mtcars[order(mtcars$cyl), ]

tmp <- file.path("<your location>/plot.emf")

devEMF::emf(tmp, family = "Arial")

ggplot(dat, aes(x=disp, y=mpg)) + geom_point() +
  labs(x = "This is the testing label!",
       y = "Testing") +
  theme(text = element_text(family = "Arial"))

dev.off()

plt <- create_plot(tmp, height = 5, width = 6, borders = c("top", "bottom", "all")) |>
  titles("Figure 1.0", "MTCARS Miles per Cylinder Plot", borders = "none") |>
  footnotes("Page [pg] of [tpg]", borders = "none", blank_row = "none", align = "right")

rpt <- create_report("<your location>/test.rtf", output_type = "RTF", font = "Arial", font_size =10) |>
  page_header("Client", "Study: XYZ") |>
  set_margins(top = 1, bottom = 1) |>
  add_content(plt) |>
  page_footer("Time", "This is the testing label!", "Page [pg] of [tpg]")


res <- write_report(rpt)

It seems that Arial font is populated successfully.
We can see that the x-axis label (generated from devEMF) looks the same as the footer (generated from MSWord):
image

I also test other fonts to make sure the original functionality is good.
Because I see you don't create tests folder, I test the cases under table. If you want unit tests in the package, I can add them.
If you have any thoughts or comments, please let me know.
Best,
Bill

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant